package com.gongpingjialoan.view;

import android.animation.Animator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.utility.DhUtil;

/**
 * Created by Administrator on 2016/10/25.
 */
public class PriceRegionView extends FrameLayout {

    private TextView lowTextView, highTextView, priceTextView, chezhuTextView;

    private View anmiView, anmi_childView;

    private ImageView arrowImageView;

    private double price = 35, min_price = 15.5, low_limit = 25.9, high_limit = 30.2, max_price = 35.1;

    private int lowWidth, highWidth, middleWidth, padding, centerWidth, anmiWidth;

    private int anmiMargin;

    public boolean running = false;

    public boolean isAnmi = false;

    TextView price_green_desT;

    public PriceRegionView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.car_detail_price, this, true);
        lowTextView = (TextView) findViewById(R.id.low_price);
        highTextView = (TextView) findViewById(R.id.high_price);
        priceTextView = (TextView) findViewById(R.id.price);
        chezhuTextView = (TextView) findViewById(R.id.chezhu_id);
        arrowImageView = (ImageView) findViewById(R.id.image);
        price_green_desT = (TextView) findViewById(R.id.price_green_des);
        anmiView = findViewById(R.id.anmi);
        anmi_childView = findViewById(R.id.anmi_child);
        lowWidth = highWidth = DhUtil.dip2px(context, 70);
        anmiWidth = DhUtil.dip2px(context, 70) / 2;
        padding = DhUtil.dip2px(context, 15);
        centerWidth = context.getResources().getDisplayMetrics().widthPixels / 2;
        middleWidth = context.getResources().getDisplayMetrics().widthPixels - 2 * lowWidth - 2 * padding;
        initData();

    }


    public void setData(String sprice, String smin_price, String slow_limit, String shigh_limit, String smax_price) {
        this.price = Double.parseDouble(sprice);
        this.min_price = Double.parseDouble(smin_price);
        this.low_limit = Double.parseDouble(slow_limit);
        this.high_limit = Double.parseDouble(shigh_limit);
        this.max_price = Double.parseDouble(smax_price);
        initData();
    }


    private void initData() {
        lowTextView.setText(low_limit + "万");
        highTextView.setText(high_limit + "万");
        priceTextView.setText(price + "万");
        if (price <= low_limit) {
            //价格过低
            arrowImageView.setImageResource(R.drawable.up_low);
            anmi_childView.setBackgroundResource(R.drawable.low_price_bg);
            chezhuTextView.setBackgroundResource(R.drawable.low_price_txt);
            priceTextView.setTextColor(getResources().getColor(R.color.low_price));
            int width = (int) ((price - min_price) / (low_limit - min_price) * lowWidth);
            int margin = Math.min(centerWidth - width - padding, centerWidth - anmiWidth - padding / 3);
            anmiMargin = -margin;
//            anmiView.animate().translationXBy(-margin).setInterpolator(new AccelerateDecelerateInterpolator()).setDuration(1000).start();
        } else if (price >= high_limit) {
            //借个过高
            arrowImageView.setImageResource(R.drawable.up_high);
            anmi_childView.setBackgroundResource(R.drawable.high_price_bg);
            chezhuTextView.setBackgroundResource(R.drawable.high_price_txt);
            priceTextView.setTextColor(getResources().getColor(R.color.high_price));
            int width = (int) ((price - high_limit) / (max_price - high_limit) * highWidth);
            int margin = Math.min(middleWidth / 2 + width, centerWidth - anmiWidth - padding / 3);
            anmiMargin = margin;
//            anmiView.animate().translationXBy(margin).setInterpolator(new AccelerateDecelerateInterpolator()).setDuration(1000).start();
        } else {
            //价格公平
            arrowImageView.setImageResource(R.drawable.up_middle);
            anmi_childView.setBackgroundResource(R.drawable.middle_price_bg);
            chezhuTextView.setBackgroundResource(R.drawable.middle_price_txt);
            priceTextView.setTextColor(getResources().getColor(R.color.middle_price));
            int width = (int) ((price - low_limit) / (high_limit - low_limit) * middleWidth);
            int margin = centerWidth - padding - lowWidth - width;
            anmiMargin = -margin;
//
        }
    }

    public void start() {
        isAnmi = true;
        anmiView.animate().translationXBy(anmiMargin).setInterpolator(new AccelerateDecelerateInterpolator()).setDuration(1000).setListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                running = true;
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                running = false;
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                running = false;
            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        }).start();

    }

    public void setAnmiViewGone() {
        anmiView.setVisibility(View.GONE);
    }

    public  void  setPriceGreenDes(String text){
        price_green_desT.setText(text);
    }

    public void reset() {
        isAnmi = false;
        anmiView.animate().translationXBy(-anmiMargin).setDuration(100).setListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                running = true;
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                running = false;
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                running = false;
            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        }).start();

    }


}
